<template>
  <div class="dashboard-container">
    <div class="welcome-section">
      <h1>欢迎回来，{{ username }}</h1>
      <p class="subtitle">这里是您的个人仪表盘，可以快速查看重要数据</p>
    </div>

    <!-- KPI 指标卡片 -->
    <div class="kpi-cards">
      <el-row :gutter="20">
        <el-col :span="4" v-for="(kpi, index) in kpiData" :key="index">
          <el-card class="kpi-card" shadow="hover">
            <div class="kpi-header">
              <span class="kpi-title">{{ kpi.title }}</span>
            </div>
            <div class="kpi-value">{{ kpi.value }}</div>
            <div class="kpi-trends">
              <div class="trend-item">
                <span>周同比</span>
                <div :class="['trend-value', kpi.weekOnWeek > 0 ? 'up' : 'down']">
                  <el-icon>
                    <CaretTop v-if="kpi.weekOnWeek > 0" />
                    <CaretBottom v-else />
                  </el-icon>
                  {{ Math.abs(kpi.weekOnWeek) }}%
                </div>
              </div>
              <div class="trend-item">
                <span>日环比</span>
                <div :class="['trend-value', kpi.dayOnDay > 0 ? 'up' : 'down']">
                  <el-icon>
                    <CaretTop v-if="kpi.dayOnDay > 0" />
                    <CaretBottom v-else />
                  </el-icon>
                  {{ Math.abs(kpi.dayOnDay) }}%
                </div>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <!-- 图表区域 -->
    <div class="charts-container">
      <el-row :gutter="20">
        <el-col :span="12" v-for="(chart, index) in charts" :key="index">
          <el-card class="chart-card" shadow="hover">
            <div class="chart-header">
              <h3>{{ chart.title }}</h3>
              <div class="chart-controls">
                <el-radio-group v-model="chart.timeRange" size="small">
                  <el-radio-button label="week">周</el-radio-button>
                  <el-radio-button label="month">月</el-radio-button>
                  <el-radio-button label="year">年</el-radio-button>
                </el-radio-group>
              </div>
            </div>
            <div class="chart-content" :ref="chart.ref"></div>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <!-- 活动面板 -->
    <div class="activity-section">
      <el-row :gutter="20">
        <el-col :span="16">
          <el-card class="activity-card" shadow="hover">
            <template #header>
              <div class="activity-header">
                <h3>最近活动</h3>
                <el-button text>查看全部</el-button>
              </div>
            </template>
            <div class="activity-list">
              <div v-for="(activity, index) in activities" :key="index" class="activity-item">
                <el-avatar :size="32" :src="activity.avatar" />
                <div class="activity-content">
                  <div class="activity-title">{{ activity.title }}</div>
                  <div class="activity-time">{{ activity.time }}</div>
                </div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="8">
          <el-card class="task-card" shadow="hover">
            <template #header>
              <div class="task-header">
                <h3>待办任务</h3>
                <el-button text>添加任务</el-button>
              </div>
            </template>
            <div class="task-list">
              <el-checkbox-group v-model="checkedTasks">
                <div v-for="(task, index) in tasks" :key="index" class="task-item">
                  <el-checkbox :label="task.id">{{ task.content }}</el-checkbox>
                  <span class="task-deadline">{{ task.deadline }}</span>
                </div>
              </el-checkbox-group>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from 'vue'
import { useRouter } from 'vue-router'
import {
  DataLine,
  ShoppingBag,
  User,
  Shop,
  Money,
  TrendCharts,
  CaretTop,
  CaretBottom
} from '@element-plus/icons-vue'
import * as echarts from 'echarts'

const router = useRouter()
const username = ref('Admin')

// KPI 数据
const kpiData = ref([
  {
    title: '总销售额',
    value: '¥ 126,560',
    weekOnWeek: 12.5,
    dayOnDay: -11.2
  },
  {
    title: '利润',
    value: '¥ 45,600',
    weekOnWeek: 15.2,
    dayOnDay: 8.5
  },
  {
    title: '订单数量',
    value: '1,280',
    weekOnWeek: -8.5,
    dayOnDay: 12.3
  },
  {
    title: '销售量',
    value: '3,600',
    weekOnWeek: 10.2,
    dayOnDay: -5.8
  },
  {
    title: '客户数量',
    value: '856',
    weekOnWeek: 5.8,
    dayOnDay: 3.2
  }
])

// 图表配置
const charts = ref([
  {
    title: '销售与利润趋势',
    ref: 'salesChart',
    timeRange: 'month'
  },
  {
    title: '订单数量趋势',
    ref: 'ordersChart',
    timeRange: 'month'
  },
  {
    title: '销售量趋势',
    ref: 'volumeChart',
    timeRange: 'month'
  },
  {
    title: '客户数量趋势',
    ref: 'customersChart',
    timeRange: 'month'
  }
])

// 活动数据
const activities = ref([
  {
    avatar: 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png',
    title: '张三完成了月度销售目标',
    time: '10分钟前'
  },
  {
    avatar: 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png',
    title: '新增订单 #12345',
    time: '30分钟前'
  }
])

// 任务数据
const tasks = ref([
  {
    id: 1,
    content: '完成月度报告',
    deadline: '今天 18:00'
  },
  {
    id: 2,
    content: '客户回访',
    deadline: '明天 10:00'
  }
])

const checkedTasks = ref([])

// 初始化图表
const initCharts = () => {
  // 这里实现图表初始化逻辑
  // 使用 echarts 创建图表
}

onMounted(() => {
  initCharts()
})
</script>

<style scoped>
.dashboard-container {
  padding: 24px;
}

.welcome-section {
  margin-bottom: 32px;
}

.welcome-section h1 {
  font-size: 28px;
  color: #303133;
  margin: 0;
  margin-bottom: 8px;
}

.subtitle {
  color: #909399;
  font-size: 14px;
  margin: 0;
}

.kpi-cards {
  margin-bottom: 32px;
}

.kpi-card {
  padding: 20px;
  height: 180px;
}

.kpi-header {
  margin-bottom: 12px;
}

.kpi-title {
  font-size: 14px;
  color: #333333;
}

.kpi-value {
  font-size: 24px;
  font-weight: bold;
  color: #333333;
  margin: 8px 0;
}

.kpi-trends {
  display: flex;
  justify-content: space-between;
}

.trend-item {
  display: flex;
  flex-direction: column;
  font-size: 12px;
}

.trend-value {
  display: flex;
  align-items: center;
  gap: 4px;
}

.trend-value.up {
  color: #FF4D4F;
}

.trend-value.down {
  color: #00C853;
}

.charts-container {
  margin-top: 20px;
}

.chart-card {
  margin-bottom: 20px;
}

.chart-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px;
  border-bottom: 1px solid #eee;
}

.chart-header h3 {
  margin: 0;
  font-size: 16px;
  color: #333333;
}

.chart-content {
  height: 300px;
}

.activity-section {
  margin-top: 20px;
}

.activity-header,
.task-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.activity-header h3,
.task-header h3 {
  margin: 0;
  font-size: 16px;
  color: #333333;
}

.activity-item {
  display: flex;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid #eee;
}

.activity-content {
  margin-left: 12px;
}

.activity-title {
  font-size: 14px;
  color: #333333;
}

.activity-time {
  font-size: 12px;
  color: #999;
  margin-top: 4px;
}

.task-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 0;
}

.task-deadline {
  font-size: 12px;
  color: #999;
}

:deep(.el-card) {
  border-radius: 8px;
  border: none;
}

:deep(.el-radio-button__inner) {
  padding: 4px 12px;
}
</style> 